using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;
namespace DAL
{
    public class tbCTDiaDiemDAL
    {
        private SqlConnection cn;
        public tbCTDiaDiemDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public tbCTDiaDiemDAL(tbCTDiaDiemDAL ctdiadiem)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbCTDiaDiem ctdiadiem)
        {
            SqlCommand cm = new SqlCommand("tbCTDiaDiem_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@cuahang", ctdiadiem.cuahang);
            cm.Parameters.AddWithValue("@diadiem", ctdiadiem.diadiem);
            cm.ExecuteNonQuery();
        }
        public void Update(tbCTDiaDiem ctdiadiem)
        {
            SqlCommand cm = new SqlCommand("tbCTDiaDiem_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@cuahang", ctdiadiem.cuahang);
            cm.Parameters.AddWithValue("@diadiem", ctdiadiem.diadiem);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tbCTDiaDiem_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@diadiem", iD);
            cm.ExecuteNonQuery();
        }
        public tbCTDiaDiemCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbCTDiaDiem_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbCTDiaDiemCollection ctddCollection = new tbCTDiaDiemCollection();
            while (dr.Read())
            {
                tbCTDiaDiem ctdiadiem = new tbCTDiaDiem();
                ctdiadiem.diadiem= int.Parse(dr.GetValue(0).ToString());
                ctdiadiem.cuahang = int.Parse(dr.GetValue(1).ToString());
                ctddCollection.Add(ctdiadiem);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ctddCollection;
        }
    }
}
